package com.online.edu.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.online.edu.eduservice.entity.EduTeacher;
import com.online.edu.eduservice.entity.QueryTeacher;
import com.online.edu.eduservice.entity.R;
import com.online.edu.eduservice.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-03-01
 */
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    @PostMapping("user/login")
    public R userLogin() {
//    public R userLogin(@RequestBody String username, @RequestBody String password) {
        return R.ok().data("token", "admin-token");
    }

    @PostMapping("user/logout")
    public R userLogout() {
        return R.ok().message("success");
    }

    @GetMapping("user/info")
    public R getUser(@RequestParam String token) {
        System.out.println(token);
        return R.ok().data("roles", "[admin]").data("introduction", "I am a super administrator")
                .data("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif")
                .data("name", "Super Admin");
    }

    /**
     * @MethodName: getAllTeacher
     * @Description: 获取所有老师列表
     * @Param: []
     * @Return: com.online.edu.eduservice.entity.R
     **/
    @GetMapping("getPageTeacher/{current}/{size}")
    public R getPageTeacher(@PathVariable("current") Integer current, @PathVariable("size") Integer size) {
        Page<EduTeacher> pageTeacher = new Page<>(current, size);
        eduTeacherService.page(pageTeacher, null);

        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        return R.ok().data("total", total).data("items", records);
    }

    /**
     * @MethodName: deleteTeacherById
     * @Description: 根据id删除Teacher(逻辑删除)
     * @Param: []
     * @Return: com.online.edu.eduservice.entity.R
     **/
    @DeleteMapping("deleteTeacher/{id}")
    public R deleteTeacherById(@PathVariable String id) {
        boolean res = eduTeacherService.removeById(id);
        return res == true ? R.ok() : R.error().message("删除失败");
    }

    /**
     * @MethodName: addTeacher
     * @Description: 添加老师
     * @Param: [eduTeacher]
     * @Return: com.online.edu.eduservice.entity.R
     **/
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = eduTeacherService.save(eduTeacher);
        return save == true ? R.ok() : R.error();
    }

    /**
     * 根据id修改老师信息
     *
     * @param eduTeacher
     * @return
     */
    @PutMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = eduTeacherService.updateById(eduTeacher);
        return save == true ? R.ok() : R.error();
    }

    //6 根据id查询讲师
    @GetMapping("getTeacherInfo/{id}")
    public R getTeacherInfo(@PathVariable String id) {
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("eduTeacher", eduTeacher);
    }

    //4 多条件组合查询带分页
    @PostMapping("getCondtionPageList/{page}/{size}")
    public R getCondtionPageList(@PathVariable Long page, @PathVariable Long size,
                                 @RequestBody(required = false) QueryTeacher queryTeacher) {
        Page<EduTeacher> pageTeacher = new Page<>(page, size);
        //调用service的方法实现条件查询带分页
        eduTeacherService.pageListCondition(pageTeacher, queryTeacher);
        //从pageTeacher对象里面获取分页数据
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        return R.ok().data("total", total).data("teacherList", records);
    }

}

